<template>
    <div class="app-container">
        <h4 class="form-header h4">基本信息</h4>
        <el-form ref="form" :model="form" label-width="80px">
            <el-row>
                <el-col :span="8" :offset="2">
                    <el-form-item label="用户昵称" prop="nickName">
                        <el-input v-model="form.nickName" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="8" :offset="2">
                    <el-form-item label="登录账号" prop="userName">
                        <el-input v-model="form.userName" disabled />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <h4 class="form-header h4">雪场信息</h4>
        <!--  -->
        <el-table v-loading="loading" ref="table" :row-key="getSnowKey" :data="infoList" @row-click="clickRow"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" :reserve-selection="true" width="55" align="center" />
            <el-table-column label="雪场名称" align="center" prop="snowField" show-overflow-tooltip />
            <el-table-column label="省市区" align="center" prop="snowAddressPath" show-overflow-tooltip />
            <el-table-column label="详细地址" align="center" prop="snowAddressDetail" show-overflow-tooltip />
            <el-table-column label="经纬度" align="center" prop="snowCenter" show-overflow-tooltip />
            <el-table-column label="雪场简介" align="center" prop="snowDesc" show-overflow-tooltip />
            <el-table-column label="雪场关闭时间" align="center" prop="snowEndTime" width="180" />
            <el-table-column label="雪场开始时间" align="center" prop="snowStartTime" width="180" />
            <!-- <el-table-column label="序号" type="index" align="center">
                <template slot-scope="scope">
                    <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
            <el-table-column label="角色编号" align="center" prop="roleId" />
            <el-table-column label="角色名称" align="center" prop="roleName" />
            <el-table-column label="权限字符" align="center" prop="roleKey" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column> -->
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <el-form label-width="100px">
            <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
                <el-button type="primary" @click="submitForm()">提交</el-button>
                <el-button @click="close()">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { getAuthRole, } from "@/api/system/user";
import { listInfo, relatedPerson, getSnowField } from '@/api/business/info'
export default {
    name: "AuthRole",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 分页信息
            total: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 选中雪场ids
            snowIds: [],
            // 角色信息
            // roles: [],
            // 用户信息
            form: {},
            // 雪场信息
            infoList: []
        };
    },
    created() {
        const userId = this.$route.params && this.$route.params.userId;
        if (userId) {
            this.loading = true;
            getAuthRole(userId).then((response) => {
                this.form = response.user;
                this.loading = false;
            });
        }
        /** 查询雪场信息列表 */
        this.getList()
        // 获取用户绑定的雪场id
        this.getSnowField(userId)
    },
    methods: {
        /** 查询雪场信息列表 */
        getList() {
            this.loading = false;
            listInfo({
                pageNum: this.queryParams.pageNum,
                pageSize: this.queryParams.pageSize,
            }).then(response => {
                response.rows.forEach(item => {
                    this.$set(item, 'snowFieldId', item.id)
                })
                this.infoList = response.rows;
                this.total = response.total;
                // console.log('infoList----------:  ', this.infoList)
            });
        },
        // 获取用户绑定的雪场id
        getSnowField(userId) {
            getSnowField(userId).then(res => {
                // console.log('res---------: ', res)
                res.data.forEach(row => {
                    // if (row.snowFieldId) {
                    //     this.$refs.table.toggleRowSelection(row);
                    // }
                    this.$refs.table.toggleRowSelection(row);
                })
            })
        },
        // 保存选中的数据编号
        getSnowKey(row) {
            return row.snowFieldId
        },

        /** 单击选中行数据 */
        clickRow(row) {
            this.$refs.table.toggleRowSelection(row);
        },

        // 多选框选中数据
        handleSelectionChange(selection) {
            // console.log('selection----------: ', selection)
            this.snowIds = selection.map((item) => item.snowFieldId);
        },
        /** 提交按钮 */
        submitForm() {
            let arr = []
            this.snowIds.forEach(element => {
                let obj = {
                    snowFieldId: element,
                    userId: this.form.userId
                }
                arr.push(obj)
            });
            relatedPerson(this.form.userId, arr).then(res => {
                // console.log('res----------: ', res)
                this.$modal.msgSuccess("分配成功");
                this.close();
            })
        },

        /** 关闭按钮 */
        close() {
            const obj = { path: "/system/user" };
            this.$tab.closeOpenPage(obj);
        },
    },
};
</script>